//
//  LCViewController+LCBaiduMobAdFirst.m
//  BaiduAd_Example
//
//  Created by admin on 2018/6/13.
//  Copyright © 2018年 iOS11luchao. All rights reserved.
//

#import "LCViewController+LCBaiduMobAdFirst.h"
#import "BaiduMobAdSDK/BaiduMobAdView.h"
#import "BaiduMobAdSDK/BaiduMobAdSetting.h"
#import <objc/runtime.h>

static void *baiduMobAdFirstKey = &baiduMobAdFirstKey;

@implementation LCBaiduMobAdFirst

@end

@implementation LCViewController (LCBaiduMobAdFirst)

- (LCBaiduMobAdFirst *)baiduMobAdFirst {
    if (objc_getAssociatedObject(self, &baiduMobAdFirstKey) == nil) {
        LCBaiduMobAdFirst *baiduMobAdFirst = [[LCBaiduMobAdFirst alloc] init];
        objc_setAssociatedObject(self, &baiduMobAdFirstKey, baiduMobAdFirst, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        return baiduMobAdFirst;
    }
    return objc_getAssociatedObject(self, &baiduMobAdFirstKey);
}
//这里注意set后面的大小写
- (void)setBaiduMobAdFirst:(LCBaiduMobAdFirst *)baiduMobAdFirst {
    objc_setAssociatedObject(self, &baiduMobAdFirstKey, baiduMobAdFirst, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)startAdViewWithHeightScale:(CGFloat)scale
                         adUnitTag:(NSString *)adUnitTag {
    //lp颜色配置
    [BaiduMobAdSetting setLpStyle:BaiduMobAdLpStyleDefault];
    //[BaiduMobAdSetting sharedInstance].supportHttps = NO;
    
    //使用嵌入广告的方法实例。
    self.baiduMobAdFirst.sharedAdView = [[BaiduMobAdView alloc] init];
    self.baiduMobAdFirst.sharedAdView.AdUnitTag = adUnitTag;
    self.baiduMobAdFirst.sharedAdView.AdType = BaiduMobAdViewTypeBanner;
    
    self.baiduMobAdFirst.sharedAdView.delegate = self;
    self.baiduMobAdFirst.adTime = 0;
    self.baiduMobAdFirst.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(doTimer) userInfo:nil repeats:YES];
}
- (void)addAdView {
    [self.baiduMobAdFirst.sharedAdView start];
    [self.view addSubview:self.baiduMobAdFirst.sharedAdView];

    [self.baiduMobAdFirst.sharedAdView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        }else{
            make.top.equalTo(self.view.mas_top);
        }
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(self.view.frame.size.width);
        make.height.mas_equalTo(self.view.frame.size.width*0.15);
    }];

}

- (void)doTimer {
    self.baiduMobAdFirst.adTime++;
    if(self.baiduMobAdFirst.adTime < 3) {
        NSLog(@"self.baiduMobAdFirst.adTime = %zd", self.baiduMobAdFirst.adTime);
    } else if(self.baiduMobAdFirst.adTime < 60) {
        [self addAdView];
    } else {
        [self clearTimer];
        [self didAdClose];
    }
}

- (void)clearTimer {
    if(self.baiduMobAdFirst.timer) {
        if([self.baiduMobAdFirst.timer isValid]) [self.baiduMobAdFirst.timer invalidate];
    }
    self.baiduMobAdFirst.timer = nil;
}

- (NSString *)publisherId {
    return  @"b3a6d662"; //@"your_own_app_id";注意，iOS和android的app请使用不同的app ID
}

- (BOOL) enableLocation {
    //启用location会有一次alert提示
    return YES;
}

- (void) willDisplayAd:(BaiduMobAdView*) adview {
    NSLog(@"delegate: will display ad");
}

- (void) failedDisplayAd:(BaiduMobFailReason) reason {
    NSLog(@"delegate: failedDisplayAd %d", reason);
}

- (void)didAdImpressed {
    NSLog(@"delegate: didAdImpressed");
    
}

- (void)didAdClicked {
    NSLog(@"delegate: didAdClicked");
}

//点击关闭的时候移除广告
- (void)didAdClose {
    [self.baiduMobAdFirst.sharedAdView removeFromSuperview];
    self.baiduMobAdFirst.sharedAdView.delegate = nil;
//    self.baiduMobAdFirst.sharedAdView = nil;   //注意一定要设置为nil
    NSLog(@"delegate: didAdClose");
}

////人群属性接口
///**
// *  - 关键词数组
// */
//- (NSArray*) keywords {
//    NSArray* keywords = [NSArray arrayWithObjects:@"测试",@"关键词", nil];
//    return keywords;
//}
//
///**
// *  - 用户性别
// */
//- (BaiduMobAdUserGender) userGender {
//    return BaiduMobAdMale;
//}
//
///**
// *  - 用户生日
// */
//- (NSDate*) userBirthday {
//    NSDate* birthday = [NSDate dateWithTimeIntervalSince1970:0];
//    return birthday;
//}
//
///**
// *  - 用户城市
// */
//- (NSString*) userCity {
//    return @"上海";
//}
//
//
///**
// *  - 用户邮编
// */
//- (NSString*) userPostalCode {
//    return @"435200";
//}
//
//
///**
// *  - 用户职业
// */
//- (NSString*) userWork {
//    return @"程序员";
//}
//
///**
// *  - 用户最高教育学历
// *  - 学历输入数字，范围为0-6
// *  - 0表示小学，1表示初中，2表示中专/高中，3表示专科
// *  - 4表示本科，5表示硕士，6表示博士
// */
//- (NSInteger) userEducation {
//    return  1;
//}
//
///**
// *  - 用户收入
// *  - 收入输入数字,以元为单位
// */
//- (NSInteger) userSalary {
//    return 10000;
//}
//
///**
// *  - 用户爱好
// */
//- (NSArray*) userHobbies {
//    NSArray* hobbies = [NSArray arrayWithObjects:@"测试",@"爱好", nil];
//    return hobbies;
//}
//
///**
// *  - 其他自定义字段
// */
//- (NSDictionary*) userOtherAttributes {
//    NSMutableDictionary* other = [[NSMutableDictionary alloc] init] ;
//    [other setValue:@"测试" forKey:@"测试"];
//    return other;
//}


@end
